package addon.w3.mdx.com;

import java.io.IOException;

import addon.w3.mdx.LEDataInputStream;
import addon.w3.mdx.MDXException;
import addon.w3.mdx.MDXParentCOM;

/**
 * GEOS						// [Geoset]
	long	nbytes;
	struct {
		long	nbytes;
		VRTX
		NRMS
		PTYP
		PCNT
		PVTX
		GNDX
		MTGC
		MATS
		long	MaterialID;
		long	SelectionGroup;
		long	Selectable		(0:none;4:Unselectable)
		float	BoundsRadius;
		float	MinExtx, MinExty, MinExtz;
		float	MaxExtx, MaxExty, MaxExtz;
		long 	nanim;
		struct {
			float	BoundsRadius;
			float	MinExtx, MinExty, MinExtz;
			float	MaxExtx, MaxExty, MaxExtz;
		} ganimations[nganim];
		UVAS
		UVBS
	} geosets[ngeos];
 * @author madjk
 *
 */
public class MDXGeosetCOM extends MDXParentCOM {
	int length = -1;
	public MDXVertexes vrtx;
	public MDXNormals nrms;
	public MDXPTYPCOM ptyp;
	public MDXPCNTCOM pcnt;
	public MDXFacesCOM pvtx;
	public MDXVertexGroupCOM gndx;
	public MDXGroupsCOM mtgc;
	public MDXMatriceCOM mats;
	public int materialID;
	public int selectionGroup;
	public int selectable;
	public float boundsRadius;
	public float minExt[],maxExt[];
	public int nanim;
	public MDXGeosetGAnimCOM ganims[] = null;
	public MDXUVASCOM uvas;
	public MDXUVBSCOM uvbs;
	
	public String getTag() {
		return "GEOS:geoset";
	}
	
	public void _decode(LEDataInputStream ins) throws IOException, MDXException {
		length = ins.readInt();
		(vrtx = new MDXVertexes()).decode(ins);
		(nrms = new MDXNormals()).decode(ins);
		(ptyp = new MDXPTYPCOM()).decode(ins);
		(pcnt = new MDXPCNTCOM()).decode(ins);
		(pvtx = new MDXFacesCOM()).decode(ins);
		(gndx = new MDXVertexGroupCOM()).decode(ins);
		(mtgc = new MDXGroupsCOM()).decode(ins);
		(mats = new MDXMatriceCOM(mtgc.groupCount)).decode(ins);
		materialID = ins.readInt();
		selectionGroup = ins.readInt();
		selectable = ins.readInt();
		boundsRadius = ins.readFloat();
		minExt = new float[3];
		maxExt = new float[3];
		for(int i=0;i<minExt.length;i++) minExt[i] = ins.readFloat();
		for(int i=0;i<maxExt.length;i++) maxExt[i] = ins.readFloat();
		nanim = ins.readInt();
		ganims = new MDXGeosetGAnimCOM[nanim];
		for(int i=0; i<ganims.length; i++) {
			ganims[i] = new MDXGeosetGAnimCOM();
			ganims[i].decode(ins);
		}
		(uvas = new MDXUVASCOM()).decode(ins);
//		debug(this.toString()+"\n"+vrtx+"\n"+pvtx+"\n"+nrms);
		
//		if(pvtx.triangles.length == 12) {
//			StringBuffer sb = new StringBuffer();
//			for(int i=0;i<pvtx.triangles.length;i++) sb.append(pvtx.triangles[i]+",");
//			sb.append("\n");
//			for(int i=0;i<vrtx.vrtxs.length;i++) {
//				sb.append("(");
//				for(int j=0;j<vrtx.vrtxs[i].vertex.length;j++) { 
//					sb.append(vrtx.vrtxs[i].vertex[j]+","); 
//				}
//				sb.append("), ");
//			}
//			sb.append("\n");
//			for(int i=0;i<nrms.nrms.length;i++) {
//				sb.append("(");
//				for(int j=0;j<nrms.nrms[i].pos.length;j++) { 
//					sb.append(nrms.nrms[i].pos[j]+","); 
//				}
//				sb.append("), ");
//			}
//			debug(sb.toString());
//		}
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("TAG : "+getTag());
		sb.append("\n\tVRTX : "+(vrtx == null ? false : true));
		sb.append("\n\tNRMS : "+(nrms == null ? false : true));
		sb.append("\n\tPTYP : "+(ptyp == null ? false : true));
		sb.append("\n\tPCNT : "+(pcnt == null ? false : true));
		sb.append("\n\tPVTX : "+(pvtx == null ? false : true));
		sb.append("\n\tGNDX : "+(gndx == null ? false : true));
		sb.append("\n\tMTGC : "+(mtgc == null ? false : true));
		sb.append("\n\tMATS : "+(mats == null ? false : true));
		sb.append("\n\tMATERIALID : "+materialID);
		sb.append("\n\tSELECTABLE : "+selectable);
		sb.append("\n\tBOUNDSRADIUS : "+boundsRadius);
		sb.append("\n\tMINEXT : "+minExt[0]+" : "+minExt[1]+" : "+minExt[2]);
		sb.append("\n\tMAXEXT : "+maxExt[0]+" : "+maxExt[1]+" : "+maxExt[2]);
		sb.append("\n\tANIMATIONNUM : "+nanim);
		sb.append("\n\tUVAS : "+(uvas == null ? false : true));
		return sb.toString();
	}

	@Override
	public boolean isImplicitTag() {
		// TODO Auto-generated method stub
		return false;
	}
}